//
//  SXRootLeftViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXRootLeftViewController.h"
#import "AppDelegate.h"
#import "SDImageCache.h"
#import "SXCollectionTableViewController.h"
#import "SXQr-CodeViewController.h"
#import "SXWeatherViewController.h"

#import "SXFeedBackViewController.h"
#import "SXAboutUsViewController.h"
@interface SXRootLeftViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *lastView;


@end

@implementation SXRootLeftViewController

- (UITableView *)myTableView
{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, kScreenWidth * 3 / 4, kScreenHeight - 200)];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    }
    return _myTableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithObjects:@"我的收藏",@"二维码扫描",@"时讯天气",@"清除缓存",@"意见反馈",@"关于时讯", nil];
    }
    return _dataArray;
}


- (UIView *)lastView
{
    if (!_lastView) {
        _lastView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 80, kScreenWidth * 3 / 4, 80)];
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        BOOL isNight = [defaults boolForKey:@"appNight"];
        UILabel *nightLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, 30)];
        if (isNight) {
            nightLabel.text = @"日间模式：";
        }
        else
        {
            nightLabel.text = @"夜间模式：";
        }
        nightLabel.font = [UIFont systemFontOfSize:15];
        nightLabel.textColor = [UIColor whiteColor];
        [_lastView addSubview:nightLabel];
        
        UIButton *nightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        nightButton.selected = isNight;
        nightButton.frame = CGRectMake(93, 4, 23, 23);
        [nightButton setBackgroundImage:[UIImage imageNamed:@"SX-yejianmoshi"] forState:(UIControlStateNormal)];
        [nightButton setBackgroundImage:[UIImage imageNamed:@"SX-rijianmoshi"] forState:(UIControlStateSelected)];
        [nightButton addTarget:self action:@selector(changeNight:) forControlEvents:(UIControlEventTouchUpInside)];
        [_lastView addSubview:nightButton];

    }
    return _lastView;
}

- (void)changeNight:(UIButton *)button
{
    button.selected = !button.selected;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:button.selected forKey:@"appNight"];
    
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    
    if (button.selected) {
        ((UILabel *)_lastView.subviews[0]).text = @"日间模式：";
        delegate.nightView.alpha = 0.4;
    }else
    {
        ((UILabel *)_lastView.subviews[0]).text = @"夜间模式：";
        delegate.nightView.alpha = 0;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.view addSubview:self.myTableView];
    [self.view addSubview:self.lastView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"SX-left%ld", indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    switch (indexPath.row) {
        case 0:
        {
            SXCollectionTableViewController *collectionVC = [[SXCollectionTableViewController alloc] init];
            [delegate.rootVC.navigationController pushViewController:collectionVC animated:YES];
        }
            break;
        case 1:
        {
            SXQr_CodeViewController *qrCodeVC = [[SXQr_CodeViewController alloc] init];
            [delegate.rootVC.navigationController pushViewController:qrCodeVC animated:YES];
        }
            break;
        case 2:
        {
            SXWeatherViewController *weatherVC = [[SXWeatherViewController alloc] init];
            [delegate.rootVC.navigationController pushViewController:weatherVC animated:YES];
        }
            break;
        case 3:
            [self clearCache];
            break;
        case 4:
        {
            SXFeedBackViewController *feedBackVC = [[SXFeedBackViewController alloc] init];
            [delegate.rootVC.navigationController pushViewController:feedBackVC animated:YES];
        }
            break;
        case 5:
        {
            SXAboutUsViewController *aboutUsVC = [[SXAboutUsViewController alloc] init];
            [delegate.rootVC.navigationController pushViewController:aboutUsVC animated:YES];
        }
            break;
            
        default:
            break;
    }

}

// 清除缓存
- (void)clearCache
{
    NSInteger d = [[SDImageCache sharedImageCache] getSize];
    CGFloat cache = d / (1024.0 * 1024.0);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"当前缓存：%.2fM", cache] message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"清除", nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDImageCache sharedImageCache] clearMemory];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
